package com.wr.controller;

import com.wr.pojo.LoginResult;
import com.wr.pojo.Resume;
import com.wr.pojo.User;
import com.wr.service.ResuteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.criteria.*;
import javax.servlet.http.HttpSession;
import java.util.Optional;

/**
 * @author 王瑞
 * @description:
 * @date 2020/2/23  20:49
 */
@Controller
@RequestMapping("/login")
public class LoginController {

    //跳转登录页面的handler
    @RequestMapping(value = "/toLogin")
    public String toLogin() {
        return "login";
    }

    //登录验证的Handler  当用户名和密码没问题时 把用户信息板寸到session中
    @ResponseBody
    @RequestMapping(value = "/login")
    public LoginResult toLogin(@RequestBody User user, HttpSession session) {

        //返回页面的pojo 用来返回当前用户是否合法的信息
        LoginResult loginResult = new LoginResult();
        //获得用户输入的用户名和密码
        String username = user.getUsername();
        String password = user.getPassword();

        //判断用户名和密码
        if ("admin".equals(username) && "admin".equals(password)){
            loginResult.setIsOk(true);
            //如果账号密码没问题  就把用户保存到session中
            session.setAttribute("user",user);
        }else {
            loginResult.setIsOk(false);
            loginResult.setMsg("用户名或者密码错误，请重新输入~！");
        }

        return loginResult;
    }
}
